var list = document.getElementById('list');

list.addEventListener('click', function (evt) {
    var target = evt.target;
    var fnt = target.dataset.fnt;
    fnt in fntList && fntList[fnt]();
});

var fntList = {
    changeFont: function () {
        chrome.tabs.insertCSS(null,
                {
                    code: "*{font-family: Consolas,'Microsoft Yahei' !important;}"
                }
            );
    },

    delCssFilter: function () {
        chrome.tabs.insertCSS(null, {
            code: "img {-webkit-filter: initial !important; filter: initial !important;}"
        })
    },

    englishQuery: function () {
        chrome.tabs.executeScript(null, {
            code: `
                var d, l, doc = document, b = doc.body, h = doc.head, s, scr;
                d = doc.createElement('div');
                d.id = 'icIBahyI-main_cont';
                l = doc.createElement('i');
                l.id = 'loading';
                s = doc.createElement('style');
                s.innerHTML = '#' + d.id + '{position:absolute;display:none;color:#00802D;background:#fff;box-shadow:0px 0px 5px;padding:8px;z-index:9999}';
                h.appendChild(s);
                b.appendChild(d);
                b.appendChild(l);

                doc.addEventListener('mouseup', function (e) {
                    var word = window.getSelection().toString().trim();
                    if (word) {
                        d.style.left = e.pageX + 'px';
                        d.style.top = e.pageY + 'px';
                        d.style.display = '';
                        load('http://open.iciba.com/huaci_new/dict.php?word=' + word);
                    }
                });

                doc.addEventListener('click', function () {
                    d.style.display = 'none';
                });

                function load(url) {
                    if (scr) {
                        scr.remove();
                    }
                    scr = doc.createElement('script');
                    scr.src = url;
                    h.appendChild(scr);
                }
            `});
    }
};